import 'package:json_annotation/json_annotation.dart';

import 'coin_type.dart';

part 'product_detail.g.dart';

@JsonSerializable()
class ProductDetail {
  final String referStoreId; // 推广店铺
  final List<Option> optionList;
  final Product product; // 商品
  final PDStore store;
  final PDStoreArea storeArea;
  final List<Picture> pictures; // 图片
  final String productModelName; // 商品名称
  final String originPrice; // 原价
  final Price validPrice; // 现价
  final Map<String, int> currentSku;
  final List<Sku> skus;
  final Mia defaultMia; // 默认上架
  final List<Mia> allMia;
  final DateTime startTime;
  final DateTime endTime;
  final DateTime serverTime;
  final List<Price> priceVos;
  final List<PDStoreArea> allArea;

  ProductDetail(
    this.referStoreId,
    this.optionList,
    this.product,
    this.store,
    this.storeArea,
    this.pictures,
    this.productModelName,
    this.originPrice,
    this.validPrice,
    this.currentSku,
    this.skus,
    this.defaultMia,
    this.allMia,
    this.startTime,
    this.endTime,
    this.serverTime,
    this.priceVos,
    this.allArea,
  );

  factory ProductDetail.fromJson(Map<String, dynamic> json) => _$ProductDetailFromJson(json);

  Map<String, dynamic> toJson() => _$ProductDetailToJson(this);
}

@JsonSerializable()
class Option {
  final int id;
  final String name;
  final List<OptionValue> productOptionValues;

  Option(this.id, this.name, this.productOptionValues);

  factory Option.fromJson(Map<String, dynamic> json) => _$OptionFromJson(json);

  Map<String, dynamic> toJson() => _$OptionToJson(this);
}

@JsonSerializable()
class OptionValue {
  final int id;

  final String value;

  OptionValue(this.id, this.value);

  factory OptionValue.fromJson(Map<String, dynamic> json) => _$OptionValueFromJson(json);

  Map<String, dynamic> toJson() => _$OptionValueToJson(this);
}

@JsonSerializable()
class Product {
  final int id;
  final String name;
  final String picture;
  @JsonKey(defaultValue: '')
  final String detail;

  Product(this.id, this.name, this.picture, this.detail);

  factory Product.fromJson(Map<String, dynamic> json) => _$ProductFromJson(json);

  Map<String, dynamic> toJson() => _$ProductToJson(this);
}

@JsonSerializable()
class PDStore {
  final int id;

  PDStore(this.id);

  factory PDStore.fromJson(Map<String, dynamic> json) => _$PDStoreFromJson(json);

  Map<String, dynamic> toJson() => _$PDStoreToJson(this);
}

@JsonSerializable()
class PDStoreArea {
  final int id;
  final String name;

  PDStoreArea(this.id, this.name);

  factory PDStoreArea.fromJson(Map<String, dynamic> json) => _$PDStoreAreaFromJson(json);

  Map<String, dynamic> toJson() => _$PDStoreAreaToJson(this);
}

@JsonSerializable()
class Picture {
  final String url;

  Picture(this.url);

  factory Picture.fromJson(Map<String, dynamic> json) => _$PictureFromJson(json);

  Map<String, dynamic> toJson() => _$PictureToJson(this);
}

@JsonSerializable()
class Price {
  final String priceType;
  final String priceTypeName;
  final CoinType coinType; // 币类型 RMB、JFB
  final String flag; // "金"
  @JsonKey(defaultValue: '未知')
  final String accountType;
  final String accountTypeName;
  final double pointCoin; // 币值额度
  final String pointCoinString; // 币值额度字符串
  final bool needPassword;

  Price(
    this.priceType,
    this.priceTypeName,
    this.coinType,
    this.flag,
    this.accountType,
    this.accountTypeName,
    this.pointCoin,
    this.pointCoinString,
    this.needPassword,
  );

  factory Price.fromJson(Map<String, dynamic> json) => _$PriceFromJson(json);

  Map<String, dynamic> toJson() => _$PriceToJson(this);
}

@JsonSerializable()
class Mia {
  final int id;
  final String name;
  final double coinAmount;
  final List<Price> pointCoins;
  final bool enableGroupPurchase; // 是否支持拼购
  final bool groupPurchasing; // 是否正在拼购
  final bool enableModify; // 是否能够修改数量
  final int minPurchaseAmount; // 最小购买数量
  final int maxAmount; // 最大购买数量，小于或等于0表示不限制

  Mia(
    this.id,
    this.name,
    this.coinAmount,
    this.pointCoins,
    this.enableGroupPurchase,
    this.groupPurchasing,
    this.enableModify,
    this.minPurchaseAmount,
    this.maxAmount,
  );

  factory Mia.fromJson(Map<String, dynamic> json) => _$MiaFromJson(json);

  Map<String, dynamic> toJson() => _$MiaToJson(this);
}

@JsonSerializable()
class Sku {
  final int miaId;
  final int stock;
  final Map<String, int> sku;

  Sku(this.miaId, this.stock, this.sku);

  factory Sku.fromJson(Map<String, dynamic> json) => _$SkuFromJson(json);

  Map<String, dynamic> toJson() => _$SkuToJson(this);
}
